﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KardexSist.UserControls.Common;
using KardexSist.Library;
using Kardex_Common;
using Kardex_Entity;
using Kardex_BussinesLogic;
using System.IO;
namespace KardexSist.UserControls.Distributor
{
    public partial class CtrlMainDistributor : CtrlMainBase, IOperations
    { 
        private List<person> ListDistribuido;
        private IOperations IOperatioSingle;
        public CtrlMainDistributor()
        {
            InitializeComponent();
            ctrlDistributor1.ShowListClick += LoadGrid;
            IOperatioSingle = (IOperations)ctrlDistributor1;          
        }

        private void LoadGrid()
        {
            try
            {
                DistribuidorFactory d = new DistribuidorFactory();
                ListDistribuido = d.GetListDistribuidor();
                grdCtrlListDistributor.DataSource = ListDistribuido;
                
                FormatColumns();
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        private void FormatColumns()
        {
            grvDistributor.Columns["IdPerson"].Visible = false;
            grvDistributor.Columns["centrodistribucion"].Visible = false;
            grvDistributor.Columns["distribuidor"].Visible = false;
            grvDistributor.Columns["lugarciexpedido"].Visible = false;
            grvDistributor.Columns["client"].Visible = false;
            grvDistributor.Columns["preventista"].Visible = false;
            grvDistributor.Columns["supervisor"].Visible = false;
            grvDistributor.Columns["lugarciexpedido"].Visible = false;


            grvDistributor.Columns["Name"].Caption = "Nombre";
            grvDistributor.Columns["Name"].VisibleIndex = 0;
            

            grvDistributor.Columns["CI"].Caption = "CI";
            grvDistributor.Columns["CI"].VisibleIndex = 1;

            grvDistributor.Columns["IdTipoCIExpedido"].Caption = "Expedido";
            grvDistributor.Columns["IdTipoCIExpedido"].VisibleIndex = 2;

            grvDistributor.Columns["IdCD"].Caption = "Centro Dist.";
            grvDistributor.Columns["IdCD"].VisibleIndex = 3;

            grvDistributor.Columns["Telephone"].Caption = "Telefono";
            grvDistributor.Columns["Telephone"].VisibleIndex = 4;
        }

        public void Add()
        {
            IOperatioSingle.Add();
        }

        public void Delete()
        {
            IOperatioSingle.Delete();
        }

        public void Save()
        {
            IOperatioSingle.Save();
        }

        public void Export()
        { 
            Utils.ExportGridView(grvDistributor); 
        }

        private void grvDistributor_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        { 
           object objDistributor= grvDistributor.GetFocusedRow();
            if (objDistributor!= null)
            {
                person personCurrent = new person() { distribuidor = new distribuidor() };
                var obj = (person)objDistributor;
                personCurrent.IdCD = obj.IdCD;
                personCurrent.IdPerson = obj.IdPerson;
                personCurrent.IdTipoCIExpedido = obj.IdTipoCIExpedido;
                personCurrent.Name = obj.Name;
                personCurrent.Telephone = obj.Telephone;
                personCurrent.Activ = obj.Activ;
                personCurrent.CI = obj.CI;
                personCurrent.distribuidor.Chofer = obj.distribuidor.Chofer;
                personCurrent.distribuidor.IdPerson = obj.IdPerson;
                ctrlDistributor1.TrigerObjPerson(personCurrent);
            } 
        }
    }
}
